﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using YourWorld.Common;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using MainGameModule.Models;
using YourWorld.Common.Infrastucture.Events;

namespace MainGameModule.ViewModels
{
    public class GameControlsExperinceViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;
        private IGameModel gameModel;

        private const double HealthMaxValue = 88;
        private const double ExperienceMaxValue = 170;

        private double health = HealthMaxValue;
        private double experience = ExperienceMaxValue;

        public GameControlsExperinceViewModel( IGameModel gameModel, IRegionManager regionManager, IUnityContainer container )
        {
            this.gameModel = gameModel;
            this.regionManager = regionManager;
            this.container = container;

            PlayerStatusManager.Instance.HealthChanged += new Action(PlayerHealthChanged);
            PlayerStatusManager.Instance.ExperienceChanged += new Action<int>( ExperienceChanged );
        }
        
        public double Health
        {
            get
            {
                return this.health;
            }
            set
            {
                if ( this.health == value )
                    return;

                this.health = value;
                this.NotifyPropertyChanged( "Health" );
            }
        }

        public double Experience
        {
            get
            {
                return this.experience;
            }
            set
            {
                if ( this.experience == value || value < 0)
                    return;

                this.experience = value;
                this.NotifyPropertyChanged( "Experience" );
            }
        }

        public void PlayerHealthChanged()
        {
            this.Health = PlayerStatusManager.Instance.Health;
        }
        
        public void ExperienceChanged( int experienceToAdd )
        {
            this.Experience -= experienceToAdd;

            if (this.Experience == 0)
            {
                this.Experience = 170;
                GameMainViewModel mainGame = this.container.Resolve<GameMainViewModel>();
                mainGame.Player.Level++;
                mainGame.Player.Experience += 170;
                PlayersRepository.Instance.SavePlayerExperience(mainGame.Player.Id, mainGame.Player.Experience, null);
                PlayersRepository.Instance.SavePlayerLevel(mainGame.Player.Id, mainGame.Player.Level, null);
                EventService.Aggregator.GetEvent<LevelFinished>().Publish(mainGame.Player.Level);
            }
        }
    }
}
